package com.carltechnology.mahjonggamecaculation.mode.impl;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

import com.carltechnology.mahjonggamecaculation.mode.IDetailGameData;
import com.carltechnology.mahjonggamecaculation.utils.Utils;

import java.util.Arrays;

/**
 * Created by WZTCM on 2016/6/27.
 */
public class GameDetailDataMode implements IDetailGameData {


    private enum DATA_COLUMN_NAME {
        COLUMN_ID("id"),
        COLUMN_INCOME1("income1"),
        COLUMN_INCOME2("income2"),
        COLUMN_INCOME3("income3"),
        COLUMN_INCOME4("income4"),
        COLUMN_COST("cost"),
        COLUMN_PERUNIT("perunit"),
        COLUMN_DATE("date");
        private String name;

        DATA_COLUMN_NAME(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    @Override
    public String toString() {
        return "GameDetailDataMode{" +
                "id=" + id +
                ", prices=" + Arrays.toString(prices) +
                ", cost=" + cost +
                ", unit=" + unit +
                ", date='" + date + '\'' +
                '}';
    }

    private int id;
    private int[] prices;
    private int cost;
    private int unit;
    private String date;

    private GameDetailDataMode(Parcel in) {
        String[] prices;
        int[] nPrices = new int[4];
        this.id = in.readInt();
        this.cost = in.readInt();
        this.unit = in.readInt();
        this.date = in.readString();
        prices = in.readString().split("_");

        nPrices[0] = Integer.parseInt(prices[0]);
        nPrices[1] = Integer.parseInt(prices[1]);
        nPrices[2] = Integer.parseInt(prices[2]);
        nPrices[3] = Integer.parseInt(prices[3]);
        this.prices = nPrices;


    }

    private GameDetailDataMode(int id, int[] prices, int cost, int unit) {
        this.id = id;
        this.prices = prices;
        this.cost = cost;
        this.unit = unit;
        date = Utils.getInstance().getTodayDate();
    }

    private GameDetailDataMode(int id, int[] prices, int cost, int unit, String date) {
        this.id = id;
        this.prices = prices;
        this.cost = cost;
        this.unit = unit;
        this.date = date;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public int[] getValues() {
        return prices;
    }

    @Override
    public int getCost() {
        return cost;
    }

    @Override
    public int getUnit() {
        return unit;
    }

    @Override
    public String getDate() {
        return date;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    //    private int id;
    //    private int [] prices;
    //    private int cost;
    //    private int unit;
    //    private String date;
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        StringBuilder sb = new StringBuilder();
        dest.writeInt(id);
        dest.writeInt(cost);
        dest.writeInt(unit);
        dest.writeString(date);
        sb.append(String.valueOf(prices[0]))
                .append("_")
                .append(String.valueOf(prices[1]))
                .append("_")
                .append(String.valueOf(prices[2]))
                .append("_")
                .append(String.valueOf(prices[3]));
        dest.writeString(sb.toString());
    }


    public static final Parcelable.Creator<GameDetailDataMode> CREATOR
            = new Creator<GameDetailDataMode>() {
        @Override
        public GameDetailDataMode createFromParcel(Parcel source) {
            return new GameDetailDataMode(source);
        }

        @Override
        public GameDetailDataMode[] newArray(int size) {
            return new GameDetailDataMode[size];
        }
    };


    @Override
    public ContentValues getContentValues() {
        ContentValues contentValues = new ContentValues();
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_ID), id);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME1), prices[0]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME2), prices[1]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME3), prices[2]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME4), prices[3]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COST), cost);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_PERUNIT), unit);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_DATE), date);
        return contentValues;
    }

    public static GameDetailDataMode transferCursor(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_ID)));
        int incomes[] = new int[4];
        incomes[0] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME1)));
        incomes[1] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME2)));
        incomes[2] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME3)));
        incomes[3] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_INCOME4)));
        int cost = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COST)));
        int perunit = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_PERUNIT)));
        String date = cursor.getString(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_DATE)));
        return new GameDetailDataMode(id, incomes, cost, perunit, date);
    }

    public static GameDetailDataMode _of(int id, int[] prices, int cost, int unit, String date) {
        return new GameDetailDataMode(id, prices, cost, unit, date);
    }

    public static GameDetailDataMode _of(int id, int[] prices, int cost, int unit) {
        return new GameDetailDataMode(id, prices, cost, unit);
    }

}
